/*
 * contrib/btree_gist/btree_date.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/builtins.h"
#include "utils/date.h"

typedef struct
{
	DateADT		lower;
	DateADT		upper;
} dateKEY;

/*
** 日期操作
*/
PG_FUNCTION_INFO_V1(gbt_date_compress);
PG_FUNCTION_INFO_V1(gbt_date_fetch);
PG_FUNCTION_INFO_V1(gbt_date_union);
PG_FUNCTION_INFO_V1(gbt_date_picksplit);
PG_FUNCTION_INFO_V1(gbt_date_consistent);
PG_FUNCTION_INFO_V1(gbt_date_distance);
PG_FUNCTION_INFO_V1(gbt_date_penalty);
PG_FUNCTION_INFO_V1(gbt_date_same);

static bool fc_gbt_dategt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(date_gt,
											DateADTGetDatum(*((const DateADT *) fc_a)),
											DateADTGetDatum(*((const DateADT *) fc_b))));
}

static bool fc_gbt_datege(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(date_ge,
											DateADTGetDatum(*((const DateADT *) fc_a)),
											DateADTGetDatum(*((const DateADT *) fc_b))));
}

static bool fc_gbt_dateeq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(date_eq,
											DateADTGetDatum(*((const DateADT *) fc_a)),
											DateADTGetDatum(*((const DateADT *) fc_b)))
		);
}

static bool fc_gbt_datele(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(date_le,
											DateADTGetDatum(*((const DateADT *) fc_a)),
											DateADTGetDatum(*((const DateADT *) fc_b))));
}

static bool fc_gbt_datelt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(date_lt,
											DateADTGetDatum(*((const DateADT *) fc_a)),
											DateADTGetDatum(*((const DateADT *) fc_b))));
}



static int fc_gbt_datekey_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	dateKEY    *fc_ia = (dateKEY *) (((const Nsrt *) fc_a)->t);
	dateKEY    *fc_ib = (dateKEY *) (((const Nsrt *) fc_b)->t);
	int			fc_res;

	fc_res = DatumGetInt32(DirectFunctionCall2(date_cmp,
											DateADTGetDatum(fc_ia->lower),
											DateADTGetDatum(fc_ib->lower)));
	if (fc_res == 0)
		return DatumGetInt32(DirectFunctionCall2(date_cmp,
												 DateADTGetDatum(fc_ia->upper),
												 DateADTGetDatum(fc_ib->upper)));

	return fc_res;
}

static float8 fc_gdb_date_dist(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	/* 我们假设差值不会溢出 */
	Datum		fc_diff = DirectFunctionCall2(date_mi,
										   DateADTGetDatum(*((const DateADT *) fc_a)),
										   DateADTGetDatum(*((const DateADT *) fc_b)));

	return (float8) Abs(DatumGetInt32(fc_diff));
}


static const gbtree_ninfo tinfo =
{
	gbt_t_date,
	sizeof(DateADT),
	8,							/* sizeof(gbtreekey8) */
	fc_gbt_dategt,
	fc_gbt_datege,
	fc_gbt_dateeq,
	fc_gbt_datele,
	fc_gbt_datelt,
	fc_gbt_datekey_cmp,
	fc_gdb_date_dist
};


PG_FUNCTION_INFO_V1(date_dist);
Datum date_dist(PG_FUNCTION_ARGS)
{
	/* 我们假设差值不会溢出 */
	Datum		fc_diff = DirectFunctionCall2(date_mi,
										   PG_GETARG_DATUM(0),
										   PG_GETARG_DATUM(1));

	PG_RETURN_INT32(Abs(DatumGetInt32(fc_diff)));
}


/**************************************************
 * 日期操作
 **************************************************/



Datum gbt_date_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}

Datum gbt_date_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_date_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	DateADT		fc_query = PG_GETARG_DATEADT(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	dateKEY    *fc_kkk = (dateKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo,
									  fcinfo->flinfo));
}


Datum gbt_date_distance(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	DateADT		fc_query = PG_GETARG_DATEADT(1);

	/* Oid		subtype = PG_GETARG_OID(3); */
	dateKEY    *fc_kkk = (dateKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_FLOAT8(gbt_num_distance(&fc_key, (void *) &fc_query, GIST_LEAF(fc_entry),
									  &tinfo, fcinfo->flinfo));
}


Datum gbt_date_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(dateKEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_date_penalty(PG_FUNCTION_ARGS)
{
	dateKEY    *fc_origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	dateKEY    *fc_newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);
	int32		fc_diff,
				fc_res;

	fc_diff = DatumGetInt32(DirectFunctionCall2(date_mi,
											 DateADTGetDatum(fc_newentry->upper),
											 DateADTGetDatum(fc_origentry->upper)));

	fc_res = Max(fc_diff, 0);

	fc_diff = DatumGetInt32(DirectFunctionCall2(date_mi,
											 DateADTGetDatum(fc_origentry->lower),
											 DateADTGetDatum(fc_newentry->lower)));

	fc_res += Max(fc_diff, 0);

	*fc_result = 0.0;

	if (fc_res > 0)
	{
		fc_diff = DatumGetInt32(DirectFunctionCall2(date_mi,
												 DateADTGetDatum(fc_origentry->upper),
												 DateADTGetDatum(fc_origentry->lower)));
		*fc_result += FLT_MIN;
		*fc_result += (float) (fc_res / ((double) (fc_res + fc_diff)));
		*fc_result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
	}

	PG_RETURN_POINTER(fc_result);
}


Datum gbt_date_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_date_same(PG_FUNCTION_ARGS)
{
	dateKEY    *fc_b1 = (dateKEY *) PG_GETARG_POINTER(0);
	dateKEY    *fc_b2 = (dateKEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
